﻿using Basic.FlowCollect;
using Basic.FlowExtendService;
using Basic.FlowLocalEvent.Model;
using Basic.FlowModel.DB;
using Basic.FlowRemoteModel;
using Basic.FlowRemoteModel.FlowApp.Model;
using Basic.FlowService.Interface;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Basic.FlowService.lmpl
{
    internal class FlowAppService : IFlowAppService
    {
        private readonly IFlowAppCollect _FlowApp;
        private readonly IFlowCollect _Flow;
        private readonly IFormService _Form;
        public FlowAppService(IFlowAppCollect flowApp, IFlowCollect flow, IFormService form)
        {
            this._Form = form;
            this._FlowApp = flowApp;
            this._Flow = flow;
        }
        public bool SetSettleFormId(long id, long? formId)
        {
            DBFlowApp app = this._FlowApp.Get(id);
            long? sourId = app.SettleFormId;
            if (this._FlowApp.SetSettleFormId(app, formId))
            {
                this._Form.LockForm(sourId, formId, id);
                return true;
            }
            return false;
        }
        public bool SetFlow(long id, AppFlowSet set)
        {
            DBFlowApp app = this._FlowApp.Get(id);
            return this._FlowApp.SetFlow(app, set);
        }

        public long Add(FlowAppSet data)
        {
            return this._FlowApp.Add(data);
        }

        public void Delete(long id)
        {
            DBFlowApp app = this._FlowApp.Get(id);
            _FlowApp.Delete(app);
            new FlowAppEvent(app).AsyncSend();
        }
        public bool Enable(long id)
        {
            DBFlowApp app = this._FlowApp.Get(id);
            if (this._FlowApp.Enable(app))
            {
                new FlowAppEvent(app).AsyncSend("Enable");
                return true;
            }
            return false;
        }

        public FlowAppBase Get(long id)
        {
            DBFlowApp app = this._FlowApp.Get(id);
            return app.ConvertMap<DBFlowApp, FlowAppBase>();
        }

        public PagingResult<FlowAppData> Query(FlowAppQuery query, IBasicPage paging)
        {
            DBFlowApp[] app = this._FlowApp.Query(query, paging, out int count);
            if (app.IsNull())
            {
                return new PagingResult<FlowAppData>();
            }
            FlowAppData[] datas = app.ConvertMap<DBFlowApp, FlowAppData>();
            long[] ids = datas.Convert(c => c.FlowId.HasValue, c => c.FlowId.Value);
            if (!ids.IsNull())
            {
                Dictionary<long, string> names = this._Flow.GetFlowName(ids);
                datas.ForEach(c =>
                {
                    if (c.FlowId.HasValue)
                    {
                        c.FlowName = names.GetValueOrDefault(c.FlowId.Value);
                    }
                });
            }
            return new PagingResult<FlowAppData>(datas, count);
        }

        public bool Stop(long id)
        {
            DBFlowApp app = this._FlowApp.Get(id);
            if (this._FlowApp.Stop(app))
            {
                new FlowAppEvent(app).AsyncSend("Stop");
                return true;
            }
            return false;
        }

        public bool Set(long id, FlowAppSet datum)
        {
            DBFlowApp app = this._FlowApp.Get(id);
            return this._FlowApp.Set(app, datum);
        }

        public FlowAppDatum[] GetAppList(FlowAppType? appType)
        {
            return this._FlowApp.Gets<FlowAppDatum>(appType, FlowAppStatus.启用);
        }

    }
}
